<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>例子33-ES6/7常用语法介绍</title>
    </head>

    <body>
        <pre>
            <code>
                // 例子33

                // 为对象添加属性和方法
                function Point(x, y) {
                    Object.assign(this, {x, y});
                }

                Object.assign(Point.prototype, {
                    getX() {
                        return this.x;
                    },
                    getY() {
                        return this.y;
                    }
                })

                let p = new Point(1, 2);
                console.log(p, p.getX(), p.getY()); // Point {x: 1, y: 2} 1 2

                //--------------------------------------------------------------------------

                // 克隆对象（不包括继承的值）
                function clone(source) {
                    return Object.assign({}, source);
                }

                // 克隆对象（包括继承的值）
                function clone(source) {
                    let proto = Object.getPrototypeOf(source);
                    return Object.assign(Object.create(proto), source);
                }

                //--------------------------------------------------------------------------

                // 合并多个对象
                const merge = (target, ...sources) => Object.assign(target, ...sources);

                //--------------------------------------------------------------------------

                // 为属性指定默认值
                const DEFAULTS = {
                    logLevel: 0,
                    outputFormat: 'html'
                }

                function process(options) {
                    options = Object.assign({}, DEFAULTS, options); // 由于浅拷贝，DEFAULTS和options最好是基本类型数据
                    // ...
                }
            </code>
        </pre>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/33.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>

</html>